package org.high5.paintmyworld;

import org.high5.model.CanvasCoordinates;

public class CanvasCoordinatesExtended extends CanvasCoordinates {
	
	double mRotation, mSpeed;
	double mDeltaX, mDeltaY, mDeltaAlt;
	
	public CanvasCoordinatesExtended(CanvasCoordinates current, CanvasCoordinates prior, CanvasCoordinates next){
		
		super(current.getX(),current.getY(),current.getAltitude());
		double x = current.getX();
		double y = current.getY();
		
		// Calculate vector
		if(next!=null){
			double unNormalisedDeltaX = next.getX()-x;
			double unNormalisedDeltaY = next.getY()-y;
			
			mSpeed = Math.sqrt(unNormalisedDeltaX*unNormalisedDeltaX+unNormalisedDeltaY*unNormalisedDeltaY);
			
			mDeltaX = (double)unNormalisedDeltaX/mSpeed;
			mDeltaY = (double)unNormalisedDeltaY/mSpeed;
			
			double nextAlt = next.getAltitude();
			double currentAlt = current.getAltitude();
			mDeltaAlt = (double)(nextAlt-currentAlt)/mSpeed;
			
			// Calculate angle
			if(prior!=null){
				double priorToCurrent = distanceOfTwoCoordinates(prior.getX(),prior.getY(),x,y);
				double priorToNext    = distanceOfTwoCoordinates(prior.getX(),prior.getY(),next.getX(),next.getY());
				double currentToNext  = distanceOfTwoCoordinates(x,y,next.getX(),next.getY());
		
				double cosTheta = priorToCurrent*priorToCurrent+currentToNext*currentToNext-priorToNext*priorToNext/(2*priorToCurrent*currentToNext);
				mRotation = (int)(Math.acos(cosTheta)*180/Math.PI);
			}else{
				mRotation = 0;
			}
		}else{
			mRotation = 0;
			mDeltaX   = 0;
			mDeltaY   = 0;
		}
		
	}
	
	public double getSpeed(){
		return mSpeed;
	}
	
	private static double distanceOfTwoCoordinates(double x1, double y1, double x2, double y2){
		return Math.sqrt(Math.abs(Math.pow((x2-x1),2.0)+Math.pow((y2-y1),2.0)));
	}
	
	public double getDeltaX(){
		return mDeltaX;
	}
	
	public double getDeltaY(){
		return mDeltaY;
	}
	
	public double getDeltaAlt(){
		return mDeltaAlt;
	}
}
